#include <moying_comm_lib/subscribe.h>
#include <moying_proto/robot_basics.pb.h>
#include <moying_proto_test/pack_log.h>

using namespace moying::communication;

//直接在主函数中sub
void robot_info_callback(const moying_proto::RobotInfo &robot_info)
{
    std::cout<<"Received robot id "<<robot_info.id()<<std::endl;
}
void point2d_callback(const moying_proto::Point2D &point2d)
{
    std::cout<<"Received Point2D with x "<<point2d.x()<<std::endl;
}

//在主函数中sub但是用某个class的函数来回调
class ExternClassTest
{
  public:
    void point2d_callback(const moying_proto::Point2D &point2d)
    {
        std::cout<<"External class test. Received Point2D with x "<<point2d.x()<<std::endl;
    }
};

//在class中sub并用该class中的回调函数
class InternalClassTest
{
  public:
    InternalClassTest(MoyingCommuSubscribePtr &sub)
    {
      sub->subscribe("test_point2d", &InternalClassTest::point2d_callback, this);
    }

    void point2d_callback(const moying_proto::Point2D &point2d)
    {
        std::cout<<"Internal class test. Received Point2D with x "<<point2d.x()<<std::endl;
    }
};



int main()
{
    std::string sch_root_dir = "./";
    log4cplus::Initializer initializer;
    std::string prop_path(sch_root_dir);
    prop_path += "configs/sub_logger.properties";
    log4cplus::PropertyConfigurator::doConfigure(prop_path); 
    logger = std::make_shared<log4cplus::Logger>(log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("main")));

    MoyingCommuSubscribePtr sub = std::make_shared<MoyingCommuSubscribe>("127.0.0.1", 50006, "sub");
    sub->setLogCall(&commLogCall, MoyingCommuLogLevels::INFO, true);//设置回调，详见logCall
    sub->setPrintPackCall(&packLogCall); //设True打印所有收发数据至console，设false不打印
    sub->run();

    sub->subscribe("test_robot_info", robot_info_callback);

    // 测试同一个信道订阅多个不同topic
    sub->subscribe("test_point2d", point2d_callback);
    SLEEP(5000);
    // 一个subscriber实例中,同一个topic只允许设一个回调函数,所以需要先unsubscribe
    sub->unsubscribe("test_point2d");

    // 测试使用class函数方式订阅
    ExternClassTest ext_class;
    sub->subscribe("test_point2d", &ExternClassTest::point2d_callback, &ext_class);
    SLEEP(5000);
    sub->unsubscribe("test_point2d");

    // 测试使用class内部形式订阅
    InternalClassTest int_class(sub);
    
    while (true)
      SLEEP(1000);
    return 0;
}
